package top.hkyzf.study.leetcode.string;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 朱峰
 * @date 2022-2-15 11:39
 */
public class 赎金信_383 {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> maps = new HashMap<>();
        for (int i = 0; i < magazine.length(); i++) {
            char ch = magazine.charAt(i);
            maps.put(ch, maps.getOrDefault(ch, 0) + 1);
        }
        for (int i = 0; i < ransomNote.length(); i++) {
            char ch = ransomNote.charAt(i);
            if (maps.containsKey(ch)) {
                maps.put(ch, maps.get(ch) - 1);
                if (maps.get(ch) == 0) {
                    maps.remove(ch);
                }
            } else {
                return false;
            }
        }
        return true;
    }

    @Test
    public void testCanConstruct () {
        String ransomNote = "aa";
        String magazine = "aab";
        System.out.println(canConstruct(ransomNote, magazine));
    }
}
